package com.outsourcing.bottle.util;

import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

/**
 * 
 * @author 06peng
 *
 */
public class LocationThread extends Thread {
	
	private LocationManager lm;
	private LocationListener locationListener = null;
	final long TIMEOUT = 10000;
	
	public BottleApplication app;
	public boolean isrun = false;
	private int iPos = 0;
	
	/**
	 * 是否gps地位成功
	 */
	private boolean isGpsSendSuccess;
	
	private Handler msgHandler;
	private Looper waitingLoop;
	
	public LocationThread(Handler msgHandler, LocationManager lm) {
		this.msgHandler = msgHandler;
		this.lm = lm;
		msgHandler.sendEmptyMessage(AppContext.START_GPS_LOCATE);
	}

	@Override
	public void run() {
		FileManager.addLog("LocationThread run()--->start");
		isGpsSendSuccess = false;
		try {
			Looper.prepare();
			waitingLoop = Looper.myLooper();
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					if (!isGpsSendSuccess) {
						msgHandler.sendEmptyMessage(AppContext.GPS_LOCATE_TIMEOUT);
						stopConnectionThread();
					}
				}
			}, TIMEOUT);
			
			System.out.println("send timeout");
			GpsStatus.Listener statusListener = new GpsStatus.Listener() {
				@Override
				public void onGpsStatusChanged(int event) {

					if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
						
					} else if (event == GpsStatus.GPS_EVENT_STOPPED) {
						
					} else if (event == GpsStatus.GPS_EVENT_STARTED) {
						
					} else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
						GpsStatus mGpsStatus = lm.getGpsStatus(null);
						Iterable<GpsSatellite> itGpsStatellites = mGpsStatus.getSatellites();
						Iterator<GpsSatellite> it = itGpsStatellites.iterator();
						iPos = 0;
						while (it.hasNext()) {
							iPos++;
						}
						FileManager.addLog(">>>>>>>>>>>>>>>>>>>>星数：" + iPos);
					}
				}
			};

			locationListener = new LocationListener() {
				@Override
				public void onLocationChanged(Location location) {
					try {
						if (location != null) {
							if (AppContext.getInstance().getLongitude()!= location.getLongitude() || AppContext.getInstance().getLatitude() != location.getLatitude()) {
								isGpsSendSuccess = true;
								AppContext.getInstance().setLongitude(location.getLongitude());
								AppContext.getInstance().setLatitude(location.getLatitude());
								msgHandler.sendEmptyMessage(AppContext.GPS_LOCATE_SUCCESS);
								stopConnectionThread();
								FileManager.addLog("GpsSendSuccess===>long=" + location.getLongitude() + " lat:" + location.getLatitude());
							}
						}

					} catch (Exception e) {
						FileManager.addLog(getClass().getSimpleName()
								+ ".java:onLocationChanged(Location location)" + "  Exception=" + e.toString());
						isGpsSendSuccess = false;
					}
				}

				@Override
				public void onProviderDisabled(String provider) {
					
				}

				@Override
				public void onProviderEnabled(String provider) {
					
				}

				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
					
				}
			};

			lm.addGpsStatusListener(statusListener);
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 2, locationListener, waitingLoop);
			Looper.loop();
			lm.removeGpsStatusListener(statusListener);
			lm.removeUpdates(locationListener);

			FileManager.addLog(getClass().getSimpleName() + ".java:run()  GpsDone");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stopConnectionThread() {
		try {
			FileManager.addLog(getClass().getSimpleName() + ": stopConnectionThread()");
			if (waitingLoop != null) {
				waitingLoop.quit();
				waitingLoop = null;
			}
		} catch (Exception e) {
			FileManager.addLog(getClass().getSimpleName()
					+ ".java:stopConnectionThread()" + "  Exception=" + e.toString());
		}
	}
}
